package com.three.box.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.three.box.R
import com.three.box.databinding.DialogFragmentModelBinding
import com.three.box.entity.Group
import com.three.box.network.AppRepository

class GroupDialogFragment: DialogFragment() {
    private lateinit var binding: DialogFragmentModelBinding

    private var callback: ((Group) -> Unit)? = null
    private var groups = mutableListOf<Group>()

    companion object {
        const val TAG = "GroupDialogFragment"
        @JvmStatic
        fun newInstance() = GroupDialogFragment()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        groups.addAll(AppRepository.instance.productGroupList)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        binding = DialogFragmentModelBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        binding.ivClose.setOnClickListener { dismiss() }
        val listAdapter = DeviceAdapter()
        with(binding.recyclerView) {
            layoutManager = LinearLayoutManager(context).apply { orientation = androidx.recyclerview.widget.RecyclerView.VERTICAL }
            addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL))
            adapter = listAdapter
        }
        listAdapter.setOnItemClickListener { adapter, _, position ->
            val group = adapter.getItem(position) as Group
            callback?.invoke(group)
            dismiss()
        }
        listAdapter.submitList(groups)
    }

    inner class DeviceAdapter : BaseQuickAdapter<Group, QuickViewHolder>() {
        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            // 返回一个 ViewHolder
            return QuickViewHolder(R.layout.item_model_dialog, parent)
        }

        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: Group?) {
            holder.setText(R.id.tv_name, item?.name)
        }
    }

    fun setCallback(callback: (group: Group) -> Unit) {
        this.callback = callback
    }

    override fun onResume() {
        super.onResume()

        val dm = resources.displayMetrics
        val window = dialog?.window
        val attrs = window?.attributes
        attrs?.width = dm.widthPixels
        attrs?.height = (dm.heightPixels * 0.5).toInt()
        attrs?.gravity = Gravity.BOTTOM
        window?.attributes = attrs
        window?.setBackgroundDrawableResource(android.R.color.transparent)
    }

}











